package de.gameduell.framework.gui.util {
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.framework.locale.LocalizableSprite;

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * @author cbra
	 */
	public class TextBackground extends LocalizableSprite {

		private const STANDARD_OFFSET:int = 10;
		
		protected var _label:TextField;
		protected var _bg:Sprite;
		protected var _html:Boolean;
		protected var _txt:String;
		protected var _offset:int;
		protected var _offsetVertical:int;
		protected var _autoSize:String = AutoSize.NONE;
		protected var _autoSizeVertical:String = AutoSize.NONE;
		protected var _center:Number;
		private var locale:Locale;
		private var _aligning:Boolean;
		private var _aligningVertical:Boolean;
		private var _resizing:Boolean;

		public function TextBackground(aSprite:Sprite,aId:String = null) {
			super(aSprite);		
			try{
				// use a blank sprite if we found no bg
				_bg	   			= getChildByName("bg") as Sprite;
				_label 			= getChildByName("label") as TextField;	
				_center 		= _bg.width/2;
				_offset 		= STANDARD_OFFSET;
				_offsetVertical = STANDARD_OFFSET;
				_aligning = false;
				_aligningVertical = false;
				_resizing = false;
			}catch(e:Error){
//				Debug.trace("TextBackground::TextBackground : Children named 'bg' or 'label' might be missing - "+e, Debug.ERROR);
			}
			locale = Locale.getInstance();

			id   = aId != null ? aId : sprite.name;
			locale.localize(this);
		}
		
		/*###################################################
		#		   			  SETTER						#
		#####################################################*/
		
		public function set autoSize(auto:String):void{
			_autoSize = auto;
			_label.autoSize = auto;
			if(auto != AutoSize.NONE){
				var format:TextFormat = new TextFormat();
				format.align = auto;
				_label.setTextFormat(format);
				resize();
			}
		}
		
		public function set autoSizeVertical(auto:String):void{
			_autoSizeVertical = auto;
			resize();
		}
		
		public function set offset(aOffset:int):void{
			_offset = aOffset;
			resize();
		}
		
		public function set offsetVertical(aOffset:int):void{
			_offsetVertical = aOffset;
			resize();
		}
		
		override public function setText(aText:String,isHtml:Boolean,aAutoSize:String):void{
			_html = isHtml;
			_txt = aText;
			autoSize = aAutoSize;
			if(_html){
				_label.htmlText = _txt;
			}else{
				_label.text = _txt;
			}
			resize();
		}
		
		/*###################################################
		#		   			  GETTER						#
		#####################################################*/
		
		public function get autoSize():String{
			return _autoSize;
		}
		
		public function get label():TextField{
			return _label;
		}
		
		public function get offset():int{
			return _offset;
		}
		
		public function get text():String{
			return _txt;
		}
		
		public function get html():Boolean{
			return _html;
		}
		
		public function get backGround():Sprite{
			return _bg;
		}
		
		
		/*###################################################
		#		   			  INTERNAL						#
		#####################################################*/
		
		/**
		 * updates _label and _bg
		 */
		protected function resize():void{		
			try{
				if(!_resizing){
					_resizing = true;
					if(_label != null){			
						if(_autoSize != AutoSize.NONE){
							_bg.width  = _label.textWidth + _offset*2;						
							align();
						}	
						
						if(_autoSizeVertical !=AutoSize.NONE){
							_bg.height = _label.height + _offsetVertical*2;
							alignVertical();
						}
					}
					_resizing = false;
				}
			}catch(e:Error){
				Debug.trace("TextBackground::update: "+e.toString(),Debug.ERROR);
			}
		}
		
		/**
		 * aligns:
		 *  - background 
		 *  - label
		 */
		protected function align():void{	
//			if(!_aligning){
				_aligning = true;
				if(label.length > 0){	
					var charOffset:Number = label.getCharBoundaries(0).x;			
					switch(_autoSize){			
						case AutoSize.CENTER:			
							_bg.x    = _center - _bg.width/2;
							_label.x = _bg.x+ (_bg.width - _label.width)/2;
							break;
						case AutoSize.LEFT:
							_bg.x    = 0;
							_label.x = _bg.x + _offset-charOffset;
							break;		
						case AutoSize.RIGHT:	
							_bg.x    = _bg.getBounds(_bg).right -_bg.width;	
							_label.x = _bg.x + (_bg.width - _label.width)/2;
					}	
				}
				_aligning = false;	
//			}
		}
		
		/**
		 * aligns:
		 *  - background 
		 *  - label
		 */
		protected function alignVertical():void{
//			if(!_aligningVertical){
				_aligningVertical = true;
				switch(_autoSizeVertical){			
					case AutoSize.CENTER:	
						_bg.y    = - _bg.height/2;		
						_label.y = _bg.y + _offsetVertical;			
						break;
					case AutoSize.UP:
						_bg.y    = 0;
						_label.y = _offsetVertical;
	//					Debug.trace("################ UP " + _bg.y + ":" + _label.y + ":" + _offsetVertical);
						break;		
					case AutoSize.BOTTOM:	
						_bg.y    = _bg.getBounds(_bg).top -_bg.height;	
						_label.y = _bg.y + _offsetVertical;
				}	
				_aligningVertical = false;	
//			}
		}
		
	}
}
